import java.util.Stack;

public class demo3 {
    public String decodeString(String s) {
        //注意 i的位置 循环结束
        Stack<StringBuilder> stack1=new Stack<>();
        //不要去使用Sting 直接使用StringBuilder更方便且简洁
        stack1.push(new StringBuilder());
        Stack<Integer>stack2=new Stack<>();
        char []arr=s.toCharArray();
        int i=0;
        int n=s.length();
        while(i<n){
            if(arr[i]>='0'&&arr[i]<='9'){
                int sum=0;
                while (arr[i]>='0'&&arr[i]<='9'){
                    sum=sum*10+arr[i]-'0';
                    i++;
                }
                stack2.add(sum);
            }
            else if(arr[i]=='['){
                i++;
                StringBuilder stringBuilder=new StringBuilder();
                while (arr[i]>='a'&&arr[i]<='z'){
                    stringBuilder.append(arr[i]);
                    i++;
                }
                stack1.add(stringBuilder);
            }
            else if(arr[i]==']'){
                int x=0;
                x=stack2.pop();
                StringBuilder stringBuilder=stack1.pop();
                while (x-->0){
                    stack1.peek().append(stringBuilder);
                }
                i++;
            }
            else {
                StringBuilder stringBuilder=new StringBuilder();
                while (i<n&&arr[i]>='a'&&arr[i]<='z') {
                    stringBuilder.append(arr[i]);
                    i++;
                }
                stack1.peek().append(stringBuilder);
            }
        }
        return stack1.peek().toString();
    }
}
